package com.workflow.generator;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.jgrapht.graph.DefaultWeightedEdge;
import org.jgrapht.graph.DirectedWeightedMultigraph;

public class Implementation {

	private static InputStreamReader isr = new InputStreamReader(System.in);
	private static BufferedReader br = new BufferedReader(isr);
	private static IOMethods iom = new IOMethods();
	private static PathSearchingEngines pse = new PathSearchingEngines();

	public static void main(String args[]) throws NumberFormatException,
			IOException {

		int source, destination;

		DirectedWeightedMultigraph<String, DefaultWeightedEdge> graph = new DirectedWeightedMultigraph<String, DefaultWeightedEdge>(
				DefaultWeightedEdge.class);
		graph = iom.initializeAGraph(graph);

		// Get the source and destination nodes from the user
		System.out.println("Enter the source node number :");
		source = Integer.parseInt(br.readLine());

		System.out.println("Enter the destination node number :");
		destination = Integer.parseInt(br.readLine());

		// Print the distance matrix
		System.out.println("\nDistance matrix:");
		iom.getDistanceMatrix(graph);

		// Print the neighbour matrix
		System.out.println("\nNeighbour matrix");
		iom.getNeighbourMatrix(graph);

		if (pse.getShortestPath(graph, Integer.toString(source),
				Integer.toString(destination)) != null) {
			// Print the shortest path
			System.out.println("\nThe shortest path : "
					+ pse.getShortestPath(graph, Integer.toString(source),
							Integer.toString(destination)));

			// Print the distance
			System.out.print("\nThe distance covered by the shortest path : "
					+ pse.getDistance(graph, Integer.toString(source),
							Integer.toString(destination)) + " m");

			// Print time delay
			System.out.println("\nTime delay for shortest path : "
					+ pse.getTimeDelay(pse.getDistance(graph,
							Integer.toString(source),
							Integer.toString(destination))) + " sec");

			// Process the alternate path graph
			@SuppressWarnings("unchecked")
			DirectedWeightedMultigraph<String, DefaultWeightedEdge> alternateGraph = (DirectedWeightedMultigraph<String, DefaultWeightedEdge>) pse
					.alternatePathGraph(graph, Integer.toString(source),
							Integer.toString(destination));

			// Print the alternate path only if shortest path exists
			System.out.println("\nAlternate path : ");
			if (alternateGraph != null
					&& (pse.getShortestPath(alternateGraph,
							Integer.toString(source),
							Integer.toString(destination)) != null)) {
				
					System.out
					.println(pse.getShortestPath(alternateGraph,
							Integer.toString(source),
							Integer.toString(destination)));
				

				// Print the distance covered by alternate path
				System.out
						.print("\nThe distance covered by the alternate path : "
								+ pse.getDistance(alternateGraph,
										Integer.toString(source),
										Integer.toString(destination)) + " m");

				// Print the time delay for the alternate path
				System.out.println("\nTime delay for alternate path : "
						+ pse.getTimeDelay(pse.getDistance(alternateGraph,
								Integer.toString(source),
								Integer.toString(destination))) + " sec");
			}
		} else {
			System.out
					.println("No path exists between source and destination !!");
		}
	}

}
